﻿using HpxWMS_Api.Entities;
using HpxWMS_Api.Models;
using HpxWMS_Api.Tools;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace HpxWMS_Api.DAL
{
    /// <summary>
    /// 账户相关的数据访问层
    /// </summary>
    public class AccountServices
    {
        private HpxWMSContext Dbcontext = new HpxWMSContext();
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName">传入的用户名</param>
        /// <param name="UserPwd">传入的密码</param>
        /// <returns></returns>
        public User FindUserNameandUserPwd(string UserName, string UserPwd)
        {

            return Dbcontext.Users.Where(User => User.UserName == UserName && User.UserPwd == UserPwd).FirstOrDefault();

        }
        /// <summary>
        /// 查找用户来自ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User FindById(int id)
        {
            return Dbcontext.Users.Find(id);
        }
        /// <summary>
        /// 添加用户到数据库
        /// </summary>
        /// <param name="ui">新用户信息</param>
        /// <returns></returns>
        public bool InsertUser(Ui ui)
        {
            User user = new User();
            user.UserName = ui.UserName;
            user.UserPwd = ui.UserPwd;
            user.NickName = ui.NickName;
            try
            {
                Dbcontext.Users.Add(user);
                // 保存操作
                Dbcontext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取一页数据
        /// </summary>
        /// <param name="uq"></param>
        public List<User> FindPage(Uq uq)
        {
            try
            {
                return Dbcontext.Users.Where(user => user.NickName.Contains(uq.SearchText)
                                      || user.UserName.Contains(uq.SearchText)
                                      || uq.SearchText == null
                                      || uq.SearchText == "")
                                  .OrderBy(user => user.Id)
                                  .Skip(uq.PageSize * (uq.PageIndex - 1))
                                  .Take(uq.PageSize)
                                  .ToList();
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获取查询总数
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public int Count(string searchText)
        {
            return Dbcontext.Users.Where(user => user.NickName.Contains(searchText)
                                      || user.UserName.Contains(searchText)
                                      || searchText == null
                                      || searchText == "").Count();
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="ue"></param>
        /// <returns></returns>
        public bool UpdataUser(Ue ue)
        {
            var edituser = Dbcontext.Users.Find(ue.Id);
            edituser.NickName = ue.NickName;
            edituser.UserName = ue.UserName;
            try
            {
                Dbcontext.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="cpwd"></param>
        /// <returns></returns>
        public bool ChangePwd(Cpwd cpwd)
        {
            var changepwd = Dbcontext.Users.Find(cpwd.Id);
            changepwd.Id = cpwd.Id;
            changepwd.UserPwd = cpwd.UserPwd;
            try
            {
                Dbcontext.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        public bool Delete(int id)
        {
            var user = Dbcontext.Users.Find(id);
            Dbcontext.Users.Remove(user);
            Dbcontext.SaveChanges();
            try
            {
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断用户是否重名
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool ExictByUserName(string userName)
        {
            return Dbcontext.Users.Where(user => user.UserName == userName).Count() > 0;
        }
        /// <summary>
        /// 通过Id判断用户名是否与更新之前相同
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ExictByUserNameandNotEqualId(string userName, int id)
        {
            return Dbcontext.Users.Where(user => user.UserName == userName && user.Id != id).Count() > 0;
        }
        /// <summary>
        /// 判断用户密码是否相同
        /// </summary>
        /// <returns></returns>
        public bool ExictByIdandNotEqualOldUserPwd(string userPwd, int id)
        {
            // 1.获取用户id,通过id获取旧密码
            var oldpwd = Dbcontext.Users.Find(id).UserPwd;
            // 2.判断旧密码和新密码是否一致
            if (oldpwd == userPwd)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool ExictByUserId(int userId)
        {
            return Dbcontext.Users.Where(user => user.Id == userId).Count() > 0;
        }
    }
}